﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Persistencia
{
    public class AccesoBD
    {

        public SqlConnection ConectarBD() {
            // 1. Crear la conexión a la base de datos. 
            string strConexion = "user id=sa;" +
                                       "password=1234;server=YAMILA\\SQLEXPRESS;" +
                                       "Trusted_Connection=yes;" +
                                       "database=etaxi; " +
                                       "connection timeout=30";
            
            //string user = ConfigurationManager.AppSettings["user"].ToString();
            //string password = ConfigurationManager.AppSettings["password"].ToString();
            //tring server = ConfigurationManager.AppSettings["server"].ToString();
            //string database = ConfigurationManager.AppSettings["database"].ToString();

            //System.Console.WriteLine(password);
            //System.Console.WriteLine(server);
            //System.Console.WriteLine(database);
            /*
            string strConexion = "user id=" + user +
                                       "password=" + password+ ";server="+ server+ ";" +
                                       "Trusted_Connection=yes;" +
                                       "database=" + database+ "; " +
                                       "connection timeout=15";
            */

            //System.Console.WriteLine("user: " + ConfigurationManager.AppSettings["user"].ToString());


            SqlConnection myConnection = new SqlConnection(strConexion);
            //abrir la conexion
            try{
                myConnection.Open();
            }catch (Exception e){
                throw new Exception("Error al intentar abrir la conexión: " + e.ToString());
            }

            return myConnection;
        }

        public void DesconectarBD(SqlConnection conection)
        {
            try
            {
                conection.Close();
            }
            catch (Exception e)
            {
                throw new Exception("Error al intentar cerrar la conexión: " + e.ToString());
            }
        }
    }
}